﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VentaElectrodomesticos.Sql;
using System.Data.SqlClient;
using System.Collections;
using VentaElectrodomesticos.Utilities;

namespace VentaElectrodomesticos.AbmUsuario
{
    public partial class FormAltaUsuario : Form
    {
        private ClaseSQL sql;
        string id;

        public FormAltaUsuario()
        {
            InitializeComponent();
            sql = new ClaseSQL();
            Usuario usuario = new Usuario();
            usuario.completarRoles(this.dataGridView1);
        }
       
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBoxNombre_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            FormSeleccionEmpleado formSeleccionEmpleado = new FormSeleccionEmpleado(this);
            formSeleccionEmpleado.ShowDialog(this);
        }

        public void setEmpleado(string id,string nombre, string apellido, string dni, string provincia, string sucursal, string tipo)
        {
            this.id = id;
            this.textBoxNombre.Text = nombre;
            this.textBoxApellido.Text = apellido;
            this.textBoxDni.Text = dni;
            this.tbProvincia.Text = provincia;
            this.tbSucursal.Text = sucursal;
            this.tbTipo.Text = tipo;
            
            this.textBoxNombre.Enabled = false;
            this.textBoxApellido.Enabled = false;
            this.textBoxDni.Enabled = false;
            this.tbProvincia.Enabled = false;
            this.tbSucursal.Enabled = false;
            this.tbTipo.Enabled = false;
        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            this.id = null;
            this.textBoxNombre.Text = null;
            this.textBoxApellido.Text = null;
            this.textBoxDni.Text = null;
            this.tbProvincia.Text = null;
            this.tbSucursal.Text = null;
            this.tbTipo.Text = null;
        }

        private String getDescripcionTipo(String idTipo) {
            try
            {
                sql.Close();
                sql.Open();
                String tipo = "";
                String defaultquery = "SELECT nomTipoUsu FROM LAG.usuarios_tipos WHERE idTipoUsu = " + idTipo;
                
                SqlDataReader reader = sql.busquedaSQLDataReader(defaultquery);
                if (reader.Read())
                {
                    tipo = reader[0].ToString();
                }
                reader.Close();
                return tipo;
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Error!");
                return null;
            }
            finally
            {
                sql.Close();
            }            
        }

        private String getDescripcionProvincia(String idProvincia)
        {
            try
            {
                sql.Close();
                sql.Open();
                String tipo = "";
                String defaultquery = "SELECT nomProvincia FROM LAG.provincias WHERE idProvincia = " + idProvincia;

                SqlDataReader reader = sql.busquedaSQLDataReader(defaultquery);
                if (reader.Read())
                {
                    tipo = reader[0].ToString();
                }
                reader.Close();
                return tipo;
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Error!");
                return null;
            }
            finally
            {
                sql.Close();
            }
        }

        private void buttonAceptar_Click(object sender, EventArgs e)
        {
            try
            {
                if (!(this.id == null) 
                    && !this.textBoxUsuario.Text.Equals("")
                    && !this.textBoxPassword.Text.Equals(""))
                {
                    if (this.textBoxPassword.Text.Equals(this.textBoxConfirmaPassword.Text))
                    {
                        Boolean bol = false;
                        if (this.dataGridView1.Rows.Count > 0)
                        {
                            for (int i = 0; i < this.dataGridView1.Rows.Count ; i++)
                            {
                                if (((this.dataGridView1.Rows[i].Cells[2]).Value) != null && (this.dataGridView1.Rows[i].Cells[2].Value.ToString() == "1")){
                                    bol = true;
                                }
                            }
                        }
                        if (bol)
                        {
                            sql.Open();
                            String[,] parametros = new String[2, 4];
                            String sp = "LAG.sp_altaUsuario";
                            parametros[0, 0] = "@id";
                            parametros[1, 0] = this.id;
                            parametros[0, 1] = "@usuario";
                            parametros[1, 1] = this.textBoxUsuario.Text;
                            parametros[0, 2] = "@password";
                            parametros[1, 2] = Utils.encriptarPassword(this.textBoxPassword.Text);
                            parametros[0, 3] = "@roles";
                            for (int i = 0; i < this.dataGridView1.Rows.Count ; i++)
                            {
                                if (((this.dataGridView1.Rows[i].Cells[2]).Value) != null && (this.dataGridView1.Rows[i].Cells[2].Value.ToString() == "1"))
                                {
                                    if (parametros[1, 3] == null)
                                        parametros[1, 3] = "¦" + this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                                    else
                                        parametros[1, 3] += "¦" + this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                                }
                            }
                            //MessageBox.Show((String)this.id + "\n" + this.textBoxUsuario.Text + "\n" + Utils.encriptarPassword(this.textBoxPassword.Text) + "\n" + parametros[1, 3].ToString(), "mensaje");
                            SqlDataReader reader = sql.ejecutarStoredProcedure(sp, parametros);
                            if (reader != null)
                            {
                                MessageBox.Show("Se ha dado de alta el usuario del empleado " + this.textBoxNombre.Text + " " + this.textBoxApellido.Text, "Success");
                                this.Close();
                            }
                        }
                        else {
                            MessageBox.Show("Debe seleccionar al menos un Rol", "Success");
                        }
                    }
                    else
                    {
                        MessageBox.Show("La contraseña ingresada es incorrecta", "Error");
                        this.textBoxPassword.Clear();
                        this.textBoxConfirmaPassword.Clear();
                    }
                }
                else
                {
                    MessageBox.Show("Faltan completar datos", "Error");
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "Error app");
            }
            finally
            {
                sql.Close();
            }
        } 
    }
}
